const request = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			// "Content-Type": "application/json",
			// "content-type" : "multipart/form-data",
			"content-type" : "application/x-www-form-urlencoded"
		}
		
		const token = uni.getStorageSync('token');
		
		if (token) {
			header['token'] = token
		}
		
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			
			success: res => {
				if (res.statusCode == 200) {
					if (res.data.code === 200 || res.data.code === "") {
						resolve(res.data)
					} else if (res.data.code == 500) {
						let msgArr = [ '请指定用户！', '没有登陆', '未登录！', null ]

						if (msgArr.includes(res.data.msg)) {
							uni.showToast({
								title: '请登录',
								icon: "error"
							})
							
							uni.setStorageSync('user', {})
							uni.setStorageSync('token', '')
							uni.setStorageSync('location', {})
							
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/mine/mine'
								})
							}, 1000)
						}
					} else if (res.data.code == 401) {
						uni.showToast({
							title: '请登录',
							icon: "error"
						})
						
						uni.setStorageSync('user', {})
						uni.setStorageSync('token', '')
						
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/mine/mine'
							})
						}, 1000)
					}else {
						uni.showToast({
							title: res.data.data || '服务器未响应',
							icon: "none"
						})
						resolve(res.data)
					}
				} else {
					reject(res)
				}
			},
			fail: res => {
				reject(res)
			}
		})
	});
}

const request2 = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": "application/json",
		}
		
		const token = uni.getStorageSync('token');
		
		if (token) {
			header['token'] = token
		}
		
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			
			success: res => {
				if (res.statusCode == 200) {
					if (res.data.code === 200 || res.data.code === "") {
						resolve(res.data)
					} else if (res.data.code == 500) {
						let msgArr = [ '请指定用户！', '没有登陆', '未登录！', null ]
						
						if (msgArr.includes(res.data.msg)) {
							uni.showToast({
								title: '请登录',
								icon: "error"
							})
							
							uni.setStorageSync('user', {})
							uni.setStorageSync('token', '')
							
							uni.switchTab({
								url: '/pages/mine/mine'
							})
						}
						
						resolve(res.data)
					}
				} else {
					reject(res)
				}
			},
			fail: res => {
				reject(res)
			}
		})
	});
}

const request3 = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": "multipart/form-data",
		}
		
		const token = uni.getStorageSync('token');
		
		if (token) {
			header['token'] = token
		}
		
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			
			success: res => {
				if (res.statusCode == 200) {
					if (res.data.code === 200 || res.data.code === "") {
						resolve(res.data)
					} else if (res.data.code == 500) {
						let msgArr = [ '请指定用户！', '没有登陆', '未登录！', null ]
						
						if (msgArr.includes(res.data.msg)) {
							uni.showToast({
								title: '请登录',
								icon: "error"
							})
							
							uni.setStorageSync('user', {})
							uni.setStorageSync('token', '')
							
							uni.switchTab({
								url: '/pages/mine/mine'
							})
						}
						
						resolve(res.data)
					}
				} else {
					reject(res)
				}
			},
			fail: res => {
				reject(res)
			}
		})
	});
}

//不带token接口请求，首页
const request4 = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			'Content-Type': 'application/json',
		}
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: res => {
				if (res.data.code === "200" || data.code === "") {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: res => {
				uni.showToast({
					title: res.data.data || '服务器未响应',
					icon: "none"
				})
				
				reject(res)
			}
		})
	});
}

// 上传附件
const upload = (url, data, filePath, method = 'POST') => {
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": "application/json",
			// "content-type" : "multipart/form-data",
		}

		const token = uni.getStorageSync('token');
		if (token) {
			header['token'] = token
		}
		uni.uploadFile({
			url: url,
			filePath: filePath,  //文件路径
			name: 'file',
			formData: data,
			method: method,
			header: header,
			success: res => {
				if (res.statusCode == 200) {
					if (res.data.code === 200 || res.data.code === "") {
						resolve(res.data)
					} else if (res.data.code === 20004 || res.data.code === 20005 || res.data.code === 401) {
						uni.removeStorageSync("storage_key")
						uni.navigateTo({
							url: '/pages/login/login'
						})
						
					} else if (!res.data.code && res.data) {
						// console.log(res.data)
						resolve(res.data)
					} else {
						uni.showToast({
							title: res.data.data || '服务器未响应',
							icon: "none"
						})
					}
				} else {
					reject(res)
					
					// uni.showToast({
					// 	title: '提交失败',
					// 	icon: "error"
					// })
				}
			},
			fail: res => {
				reject(res)
			}
		})
	});
}

module.exports = {
	request: request,
	request2: request2,
	request3: request3,
	upload: upload
}
